package us.nchu.tomtiger.controller;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.enums.ResultEnum;
import us.nchu.tomtiger.model.Project;
import us.nchu.tomtiger.model.Result;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.model.UserShow;
import us.nchu.tomtiger.service.CollectionService;
import us.nchu.tomtiger.service.ProjectService;
import us.nchu.tomtiger.service.UserService;
import us.nchu.tomtiger.service.UserShowService;
import us.nchu.tomtiger.utils.ResultUtil;
import us.nchu.tomtiger.utils.UserUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Controller
@RequestMapping("/project")
public class ProjectController {
    @Resource
    private UserShowService userShowService;
    @Resource
    private UserService userService;
    @Resource
    private ProjectService projectService;
    @Resource
    private CollectionService collectionService;

    @GetMapping("/{projectId}")
    public ModelAndView doGet(HttpServletRequest httpServletRequest, @PathVariable String projectId) {
        Project project;

        try {
            project = projectService.getProjectById(projectId);
        }catch (EmptyResultDataAccessException e) {
            // 找不到这个project id
            return new ModelAndView("project-404");
        }

        User user = userService.getUserByPhoneNumber(project.getPhoneNumber());
        UserShow userShow = userShowService.getUserShowByUserId(user.getPhoneNumber());

        ModelAndView modelAndView = new ModelAndView("project");
        modelAndView.addObject("projectId", projectId);
        modelAndView.addObject("name", project.getName());
        modelAndView.addObject("owner", userShow.getId());
        modelAndView.addObject("ownerNickName", user.getNickName());
        modelAndView.addObject("type", project.getType());
        modelAndView.addObject("contactType", project.getContactType());
        modelAndView.addObject("contact", project.getContact());
        modelAndView.addObject("intro", project.getIntro());
        modelAndView.addObject("field", project.getField());

        // 是否已经收藏过这个项目
        if (!UserUtil.isLogin(httpServletRequest)) {
            modelAndView.addObject("isCollected", 0);
        }
        else {
            if (collectionService.isCollected(UserUtil.getUserPhoneNumber(httpServletRequest), Integer.parseInt(projectId))) {
                System.out.println("isCollected = 1");
                modelAndView.addObject("isCollected", 1);
            }
            else {
                modelAndView.addObject("isCollected", 0);
            }
        }
        return modelAndView;
    }

    @PutMapping
    @ResponseBody
    public Result updateProjectInfo(HttpServletRequest httpServletRequest, Project project) {
        project.setPhoneNumber((String) httpServletRequest.getSession().getAttribute("userId"));

        projectService.createUncheckedProject(project);

        return ResultUtil.success("创建项目需要管理员审核后方可查看，请耐心等待");
    }

    @PostMapping
    @ResponseBody
    public Result modifyProject(HttpServletRequest httpServletRequest, Project project) {
        // 判断要修改的项目是否属于该用户
        if (!checkAccess(httpServletRequest, Integer.toString(project.getId()))) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_PROJECT_NO_ACCESS);
        }

        projectService.updateProjectInfo(project);

        return ResultUtil.success("修改成功");
    }

    @DeleteMapping("/{projectId}")
    @ResponseBody
    public Result deleteProject(HttpServletRequest httpServletRequest, @PathVariable String projectId) {
        // 判断要删除的项目是否属于该用户
        if (!checkAccess(httpServletRequest, projectId)) {
            return ResultUtil.getNoObjectInstance(ResultEnum.ERROR_PROJECT_NO_ACCESS);
        }

        projectService.deleteProjectById(projectId);
        return ResultUtil.success("删除成功");
    }

    private boolean checkAccess(HttpServletRequest httpServletRequest, String projectId) {
        String userPhoneNumber = UserUtil.getUserPhoneNumber(httpServletRequest);
        Project project = projectService.getProjectById(projectId);
        return project.getPhoneNumber().equals(userPhoneNumber);
    }
}
